﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RonYee.Models;
using RonYee.Service;

namespace RonYee.Controllers
{
    public class FinancialController : Controller
    {
        //
        // GET: /Financial/

        RongeDb db = new RongeDb();

        [Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            RonYee.Models.MvcPage Model = new MvcPage();
            Model.CurrentPageIndex = 1;  //当前页 默认第一页

            int SkinCount = (Model.CurrentPageIndex - 1) * Model.PageSize;

            //测试用例。实际用自己的逻辑获取数据
            List<Financial> datesource = GetDataSource();//要分页的数据源
            Model.FinancialList = datesource.Where(r => r.Id >= 0).Skip(SkinCount).Take(Model.PageSize).ToList();
            //测试用例。实际用自己的逻辑获取数据

            //是否开启Ajax分页
            Model.IsAjax = true;
            Model.TotalCount = datesource.Count;//获取记录总数

            //获取分页数据
            Model.FinancialList = datesource.Where(r => r.Id >= 0).Skip(SkinCount).Take(Model.PageSize).ToList();
            return View(Model);

        }

        private List<Financial> GetDataSource()
        {
            var fin = from b in db.Financials
                       orderby b.AddTime descending
                       select b;
            List<Financial> model = new List<Financial>();
            foreach (var item in fin)
            {
                model.Add(item);
            }
            return model;
        }

        [Authorize(Roles = "Admin")]
        public ActionResult PageIndex(RonYee.Models.MvcPage Model)
        {

            //分页数据源。自己写逻辑获取。


            int SkinCount = (Model.CurrentPageIndex - 1) * Model.PageSize;

            //测试用例。实际用自己的逻辑获取数据
            List<Financial> datesource = GetDataSource();//要分页的数据源
            Model.FinancialList = datesource.Where(r => r.Id >= 0).Skip(SkinCount).Take(Model.PageSize).ToList();
            //测试用例。实际用自己的逻辑获取数据

            Model.TotalCount = datesource.Count;//获取记录总数
            if (Request.IsAjaxRequest())  //是AJAX请求的话返回AJAX分页数据
            {
                Model.IsAjax = true;
                return View(Model);
            }
            else
            {
                Model.IsAjax = false;
                return this.PartialView("Index", Model);
            }
            //return View(Model);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Details(int id) 
        {
            var fin = db.Financials.Find(id);

            int orderid = Convert.ToInt32(fin.OrderId);

            var order = from o1 in db.Orders
                        where o1.OrderId == orderid
                        select o1;

            var orderDetails = from o in db.OrderDetails
                               where o.OrderId == orderid
                               select o;

            ViewData["order"] = order;
            ViewData["details"] = orderDetails;
            return View(fin);
        }

    }
}
